package com.edu.service.impl;

import com.edu.dao.AnnouncementsDao;
import com.edu.dao.impl.AnnouncementsDaoImpl;
import com.edu.entity.AnnouncementEntity;
import com.edu.service.AnnouncementsService;
import com.edu.utils.JwtUtils;

import java.util.List;
import java.util.Map;

/**
 * com.edu.service.impl
 * 2024/7/14 22:39
 *
 * @author Fancy
 * @version 1.0
 */
public class AnnouncementsServiceImpl implements AnnouncementsService {
    AnnouncementsDao announcementsDao = new AnnouncementsDaoImpl();
    @Override
    public int createAnnouncement(AnnouncementEntity announcement, String token) {
        try {
            Map<String, Object> user = JwtUtils.validateToken(token);
            if (user.get("role").equals("admin")){
                //管理员有权删除所有评论
                return announcementsDao.createAnnouncement(announcement);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public List<AnnouncementEntity> readAllAnnouncements() {
        return announcementsDao.readAllAnnouncements();
    }

    @Override
    public int updateAnnouncementStatus(String status, int announcementsId, String token) {
        try {
            Map<String, Object> user = JwtUtils.validateToken(token);
            if (user.get("role").equals("admin")){
                //管理员有权删除所有评论
                return announcementsDao.updateStatus(announcementsId,status);
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }
}
